package logger

import (
	"fmt"
	"os"
	"time"

	"github.com/op/go-logging"
)

var Log = logging.MustGetLogger("chain-sync")

var format = logging.MustStringFormatter(
	`%{color}%{time:15:04:05.000} %{shortfunc} %{shortfile} > %{level:.4s} %{id:03x}%{color:reset} %{message}`,
)

func init() {
	logFile, err := os.OpenFile(fmt.Sprintf("./log/log_%s.txt", time.Now().Format("2006-01-02 15:04:05")), os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		fmt.Println(err)
	}
	backend1 := logging.NewLogBackend(logFile, "", 0)
	backend2 := logging.NewLogBackend(os.Stderr, "", 0)

	backend2Formatter := logging.NewBackendFormatter(backend2, format)
	backend1Leveled := logging.AddModuleLevel(backend1)
	backend1Leveled.SetLevel(logging.INFO, "")
	logging.SetBackend(backend1Leveled, backend2Formatter)

}
